<template>
	<view class="personal_bg">
		<view class="avatar_nickName">
			<image :src="avatarUrl" mode=""></image>
			<view class="nickName">{{nickName}}</view>
		</view>
		<view class="tab_selector">
			<view class="tab_selector_per">
				<text class="tab_selector_per_font">我的收藏</text>
				<uni-icons type="forward" class="tab_selector_per_icon"></uni-icons>
			</view>
			<view class="tab_selector_per">
				<text class="tab_selector_per_font">发表动态</text>
				<uni-icons type="forward" class="tab_selector_per_icon"></uni-icons>
			</view>
			<view class="tab_selector_per">
				<text class="tab_selector_per_font">最新资讯</text>
				<uni-icons type="forward" class="tab_selector_per_icon"></uni-icons>
			</view>
			<view class="tab_selector_per">
				<text class="tab_selector_per_font">帮助中心</text>
				<uni-icons type="forward" class="tab_selector_per_icon"></uni-icons>
			</view>
			<view class="tab_selector_per">
				<text class="tab_selector_per_font">个人设置</text>
				<uni-icons type="forward" class="tab_selector_per_icon"></uni-icons>
			</view>
			<button @click="userLogin" open-type="getUserInfo" class="login" v-if="!isLogin">登陆</button>
			<button @click="logout" class="logout" v-else>退出登陆</button>
		</view>
	</view>
</template>

<script>
	import {
		getCurrentTime
	} from '../../static/js/utils.js'
	export default {
		data() {
			return {
				avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
				nickName: '微信用户',
				isLogin: false
			}
		},
		onLoad() {
			// 获取登陆状态
			console.log(uni.getStorageSync("lastLoginTime"));
			if (uni.getStorageSync("lastLoginTime")) {
				const lastLoginTime = uni.getStorageSync("lastLoginTime"); // 上次登陆时间
				var currentTime = new Date();
				// 距离上次登陆相差多少分钟
				var minute = Math.floor((currentTime.getTime() - lastLoginTime.getTime()) / (60 * 1000));
				if (minute > 60) {
					// 超过一个小时了，重新登陆
					this.isLogin = false;
					this.avatarUrl =
						'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132'
					this.nickName = '微信用户';
				} else {
					this.isLogin = true;
					this.avatarUrl = uni.getStorageSync("avatarUrl");
					this.nickName = uni.getStorageSync("nickName")
				}
			} else {
				this.isLogin = false;
				this.avatarUrl =
					'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132'
				this.nickName = '微信用户';
			}
		},
		methods: {
			// 用户登陆
			userLogin() {
				var result = {};
				uni.showLoading({
					title: "获取中"
				})
				uni.getUserProfile({
					desc: '用户完善信息',
					success(res) {
						result = res;
					}
				})
				setTimeout(() => {
					uni.hideLoading()
					this.avatarUrl = result.userInfo.avatarUrl
					console.log(this.avatarUrl);
					this.nickName = result.userInfo.nickName
					this.isLogin = true
					// 存储
					uni.setStorageSync("lastLoginTime", new Date())
					uni.setStorageSync("isLogin", this.isLogin)
					uni.setStorageSync("avatarUrl", this.avatarUrl)
					uni.setStorageSync("nickName", this.nickName)
					console.log(result);
					if (result.userInfo) {
						console.log("信息获取成功");
						uni.showToast({
							title: '获取信息成功',
							icon: 'success',
							duration: 1000
						})
						setTimeout(()=>{
							uni.hideToast();
						},1000)
					} else {
						uni.showToast({
							title: '获取信息失败，登陆失败',
							icon: 'error',
							duration: 1000
						})
						setTimeout(()=>{
							uni.hideToast();
						},1000)
					}
				}, 2000)
			},
			// 退出登陆
			logout() {
				const _this = this;
				uni.showModal({
					title: '提示',
					content: '是否确认退出',
					success(res) {
						if (res.confirm) {
							uni.clearStorageSync();
							_this.isLogin = false;
							_this.avatarUrl =
								'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132'
							_this.nickName = '微信用户';
						}
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.personal_bg {
		min-height: 100vh;
		width: 100%;
		background-color: #fffae8;
	}

	.avatar_nickName {
		height: 150px;
		width: 100%;
		padding-top: 30px;
		text-align: center;
	}

	.avatar_nickName>image {
		width: 50px;
		height: 50px;
		border-radius: 50%;
	}

	.nickName {
		font-size: 14px;
		color: #949494;
	}

	.login {
		font-size: 16px;
		color: #55aaff;
	}

	.tab_selector_per {
		width: 100%;
		height: 50px;
		background-color: #ffffff;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.tab_selector_per_font {
		position: relative;
		left: 10px;
		font-size: 14px;
		color: #6a6a6a;
	}

	.tab_selector_per_icon {
		position: relative;
		right: 10px;
	}
	.logout{
		font-size: 16px;
		color: #eb0000;
	}
</style>
